<template>
  <div class="app-container">
    <el-card class="search-card">
      <el-form
        size="small"
        :model="queryParams"
        ref="queryForm"
        class="formBox"
        label-width="88px"
      >
        <el-row :gutter="0">
          <el-col :span="5">
            <el-form-item label="商家名称" prop="merchantName" class="width_100">
              <el-input
                v-model="queryParams.merchantName"
                placeholder="请输入商家名称"
                clearable
                style="width: 100%"
                maxlength="50"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="联系人" prop="contactPerson">
              <el-input
                v-model="queryParams.contactPerson"
                placeholder="请输入联系人"
                maxlength="50"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="联系电话" prop="contactPhone">
              <el-input
                v-model="queryParams.contactPhone"
                placeholder="请输入联系电话"
                maxlength="11"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="启用状态" prop="isEnabled" class="width_100">
              <el-select
                v-model="queryParams.isEnabled"
                placeholder="请选择启用状态"
                style="width: 100%"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.sys_open_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item>
              <div class="dis aic">
                <div class="flex_1"></div>
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="handleQuery"
                >
                  搜索
                </el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
                  重置
                </el-button>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="search-card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['merchant:merchantList:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['merchant:merchantList:export']"
            >导出</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
    </el-card>
    <el-card class="table-card">
      <el-table v-loading="loading" :data="merchantListList" stripe border>
        <el-table-column label="学校名称" align="center" prop="schoolName" />
        <el-table-column label="商家名称" align="center" prop="merchantName" />
        <el-table-column label="商家编号" align="center" prop="merchantCode" />
        <el-table-column label="联系人" align="center" prop="contactPerson" />
        <el-table-column label="联系电话" align="center" prop="contactPhone" />
        <el-table-column label="商家地址" align="center" prop="address" />
        <el-table-column label="启用状态" align="center" prop="isEnabled">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_open_status" :value="scope.row.isEnabled" />
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createdAt, "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['merchant:merchantList:edit']"
            >
              修改
            </el-button>
            <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['merchant:merchantList:remove']"
              >删除
            </el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改商家列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="queryForm" label-width="120px">
        <el-row>
          <el-col :span="20">
            <el-form-item label="学校名称" prop="schoolId" class="is-required">
              <el-select
                v-model="queryForm.schoolId"
                placeholder="请选择学校名称"
                clearable
                style="width: 100%"
                @change="getCampusListsFun"
              >
                <el-option
                  v-for="(item, index) in schoolList"
                  :key="index"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="商家名称" prop="merchantName" class="is-required">
              <el-input
                v-model="queryForm.merchantName"
                placeholder="请输入商家名称"
                maxlength="50"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="商家编号" prop="merchantCode" class="is-required">
              <el-input
                v-model="queryForm.merchantCode"
                placeholder="请输入商家编号"
                maxlength="50"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="联系人" prop="contactPerson" class="is-required">
              <el-input
                v-model="queryForm.contactPerson"
                placeholder="请输入联系人"
                maxlength="50"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="联系电话" prop="contactPhone" class="is-required">
              <el-input
                v-model="queryForm.contactPhone"
                placeholder="请输入联系电话"
                maxlength="11"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="启用状态" prop="isEnabled" class="is-required">
              <el-select
                v-model="queryForm.isEnabled"
                placeholder="请选择启用状态"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dict.type.sys_open_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="商家地址" prop="address" class="is-required">
              <el-input
                v-model="queryForm.address"
                type="textarea"
                placeholder="请输入内容"
                maxlength="100"
                show-word-limit
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMerchantList,
  getMerchantList,
  delMerchantList,
  addMerchantList,
  updateMerchantList,
} from "@/api/merchant/merchantList";
import { schoolListAll, getCampusLists } from "@/api/campus/all.js";
export default {
  name: "MerchantList",
  dicts: ["sys_open_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商家列表表格数据
      merchantListList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        merchantCode: null,
        merchantName: null,
        associatedSchool: null,
        isEnabled: null,
        contactPerson: null,
        contactPhone: null,
      },
      // 表单参数
      queryForm: {
        schoolId: null,
        merchantName: null,
        merchantCode: null,
        contactPerson: null,
        contactPhone: null,
        address: null,
        isEnabled: null,
      },
      schoolList: [],
    };
  },
  created() {
    this.getList();
    this.schoolListAllFun();
  },
  activated() {
    this.$forceUpdate();
  },
  methods: {
    schoolListAllFun() {
      schoolListAll().then((res) => {
        if (res.code == 200) {
          this.schoolList = res.data;
        }
      });
    },
    getCampusListsFun(options) {
      getCampusLists({ schoolId: options }).then((res) => {
        if (res.code == 200) {
          // this.schoolList = res.data;
        }
      });
    },
    /** 查询商家列表列表 */
    getList() {
      this.loading = true;
      listMerchantList(this.queryParams).then((response) => {
        this.merchantListList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.queryForm = {
        schoolId: null,
        merchantName: null,
        merchantCode: null,
        contactPerson: null,
        contactPhone: null,
        address: null,
        isEnabled: null,
      };
      this.resetForm("queryForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商家列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getMerchantList(id).then((response) => {
        this.queryForm = response.data;
        console.log(this.queryForm);
        this.open = true;
        this.title = "修改商家列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      var data = this.queryForm;
      var that = this;
      if (!data.schoolId) {
        return that.$modal.msgError("请选择学校");
      }
      if (!data.merchantCode) {
        return that.$modal.msgError("请输入商户编码");
      }
      if (!data.merchantName) {
        return that.$modal.msgError("请输入商家名称");
      }
      if (!data.contactPerson) {
        return that.$modal.msgError("请输入联系人");
      }
      if (!data.contactPhone) {
        return that.$modal.msgError("请输入联系电话");
      }
      if (!data.address) {
        return that.$modal.msgError("请输入商家地址");
      }
      data.isEnabled = data.isEnabled + "";
      if (!data.isEnabled) {
        return that.$modal.msgError("请选择启用状态");
      }
      if (data.id != null) {
        updateMerchantList(data).then((response) => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      } else {
        addMerchantList(data).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id;
      this.$modal
        .confirm('是否确认删除商家为"' + row.merchantName + '"的数据项？')
        .then(function () {
          return delMerchantList(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "merchant/merchantList/export",
        {
          ...this.queryParams,
        },
        "商家" + `_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
